require 'net/http'

module Installr::Manager
  class Tomcat < Base
    def request(command, app_name = nil, war_file = nil)
      http = Net::HTTP.new("localhost", @options[:manager_port])
      http.read_timeout = @options[:manager_timeout].to_i
      path = "/manager/text/"+command.to_s
      if app_name
        path += "?path=/"+app_name
      end
      if app_name && war_file
        path += "&war=file:" + war_file
      end

      response = http.start { |http|
        request = Net::HTTP::Get.new(path)
        request.basic_auth @options[:manager_user], @options[:manager_password]
        http.request(request)
      } 
      return response
      rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
        puts e
        @log.error("[tomcat:request] #{e}")
        return nil
    end

    def deploy(app_name, war_file)
      super(app_name)
      response = request(:deploy, app_name, war_file)
      if response
        puts response.body if @options[:verbose]
        response.body
      else
        @log.error("[tomcat:deploy] service not available")
        "service not available"
      end
    end

    def undeploy(app_name)
      super(app_name)
      response = request(:undeploy, app_name)
      if response
        puts response.body if @options[:verbose]
        response.body
      else
        @log.error("[tomcat:undeploy] service not available")
        "service not available"
      end
    end

    def start(app_name)
      super(app_name)
      response = request(:start, app_name)
      if response
        puts response.body if @options[:verbose]
        response.body
      else
        @log.error("[tomcat:start] service not available")
        "service not available"
      end
    end

    def stop(app_name)
      super(app_name)
      response = request(:stop, app_name)
      if response
        puts response.body if @options[:verbose]
        response.body
      else
        @log.error("[tomcat:stop] service not available")
        "service not available"
      end
    end

    def reload(app_name)
      super(app_name)
      response = request(:reload, app_name)
      if response
        puts response.body if @options[:verbose]
        response.body
      else
        @log.error("[tomcat:reload] service not available")
        "service not available"
      end
    end

    def list
      super
      response = request(:list)
      if response
        puts response.body if @options[:verbose]
        response.body.lines.to_a[1..-1].join
      else
        @log.error("[tomcat:list] information not available")
        "information not available"
      end
    end

    def serverinfo
      super
      response = request(:serverinfo)
      if response
        puts response.body if @options[:verbose]
        response.body.lines.to_a[1..-1].join
      else
        @log.error("[tomcat:serverinfo] information not available")
        "information not available"
      end
    end
  end
end
